# -*- coding:utf-8 -*-
# -1- 对各种计数提交需要计数去重, cache即可, action_uid_id
# -2- 对评论标记删除的东东进行替换
import commentDao, json, time, hashlib
import os
os.sys.path.append('../../utils/constants/')
from constants import *

class commentLogic(object):
    "logics of comment"
    def __init__(self, dict_comment_table, db_conn, memcache_conn):
        # 配置
        self.__thread_table = dict_comment_table['thread_table']
        self.__dao = commentDao.commentDao(dict_comment_table, db_conn)
        self.__mc = memcache_conn
        self.__mc_expire_time = 10*86400

        self.__delete_text = '该评论已被删除'

    def ding_comment(self, ding_item):
        # ding_item has 'comment_id', 'uip', 'ding'
        # 自己能顶自己嘛。。客户端控制一下
        key = 'ding:%s:%u:%u:%s' % (self.__thread_table, ding_item['thread_id'], ding_item['comment_id'], ding_item['uip'],)
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if not self.__mc.get(mckey):
            self.__mc.add(mckey, int(time.time()), self.__mc_expire_time)
            return self.__dao.ding_comment(ding_item)
        else:
            ret = {}
            ret['retcode'] = E_DUP_OPERATION
            ret['message'] = 'ding duplicate operation table[%s] uip[%s] cmt_id[%u].' %\
                    (self.__thread_table, ding_item['uip'], ding_item['comment_id'],)
            return ret
    def add_comment(self, comment_item):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        if type(comment_item['content']) != type({}):
            ret['retcode'] = E_VALUE_TYPE_ERROR
            ret['message'] = "comment_item field [content] not dict"
            return ret
        elif not comment_item['content'].has_key('reply'):
            ret['retcode'] = E_KEY_NOT_EXIST
            ret['message'] = "comment_item field content not has key reply"
            return ret
        elif 0 == len(comment_item['content']['reply']):
            ret['retcode'] = E_COMMENT_REPLY_ZERO_LENGTH
            ret['message'] = "comment_item field 'reply' zero-length"
            return ret
        else:
            # comment中content的格式
            # 'reply'         -> 回复的内容
            # 'quote_content' -> 被引用的内容
            # 'quote_uid'     -> 被引用的用户id
            # 'quote_uname'   -> 被引用的用户名
            # 转化为string
            comment_item['content'] = json.dumps(comment_item['content'])

        return self.__dao.add_comment(comment_item)

    def del_comment(self, comment_item,):
        return self.__dao.del_comment(comment_item)

    def get_comment(self, thread_id, pageno):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = 'OK'
        # 替换已经标记删除的评论
        cmt_list = self.__dao.get_comment(thread_id, pageno)
        for step in range(len(cmt_list)):
            cmt_list[step]['content'] = json.loads(cmt_list[step]['content'])
            if cmt_list[step]['status'] != 0:
                cmt_list[step]['content']['reply'] = self.__delete_text
        ret['retbody'] = cmt_list
        return ret
